package com.yqb.study.modular.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yqb.study.common.exception.GlobalException;
import com.yqb.study.common.from.LoginFrom;
import com.yqb.study.common.session.IRedisSession;
import com.yqb.study.constant.CodeMsgEnum;
import com.yqb.study.constant.key.ICookieKey;
import com.yqb.study.modular.persistence.dao.UserMapper;
import com.yqb.study.modular.persistence.model.User;
import com.yqb.study.modular.service.IUserService;
import com.yqb.study.util.CookieUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public String userLogin(LoginFrom loginVo, HttpServletRequest request, HttpServletResponse response, IRedisSession sessionRedis) {
		
		if (sessionRedis.getSessionRedis() != null && sessionRedis.getSessionRedis().getUser() != null) {
			// 延长登录有效期
			sessionRedis.addCookieSession(response, sessionRedis.getSessionRedis().getToken(), sessionRedis.getSessionRedis().getUser());
			return sessionRedis.getSessionRedis().getToken();
		}
		
		Wrapper<User> userWrapper = new EntityWrapper<>();
		userWrapper = userWrapper.eq("phone", loginVo.getMobile());
		List<User> userList = userMapper.selectList(userWrapper);
		
		// 该手机号尚未注册
		if (userList.isEmpty()) {
			throw new GlobalException(CodeMsgEnum.LOGIN_NO_PHONE, loginVo.getMobile());
		}
		
		// 密码错误
		if (!userList.get(0).getPassword().equals(loginVo.getPassword())) {
			throw new GlobalException(CodeMsgEnum.LOGIN_NO_PASSWD, loginVo.getPassword());
		}

		// 1.首次登陆是保存session内容到redis,2.已经登录此处是延长session有效时间
		// 防止反复刷新登录页面,多次生成不同token,用户token存在,说明已经登录过,沿用该token,延长登录有效期
//		String token = CookieUtil.getToken(request, ICookieKey.TOKEN_KEY);
//		if (StringUtils.isEmpty(token)) {
//			// token不存在,生成新的token,进行用户已登录缓存
//			token = UUID.randomUUID().toString();					
//		}
		
		
		// 同一个浏览器token是一样的
		String token = UUID.randomUUID().toString();

//		// 1.首次登陆是保存session内容到redis,2.已经登录此处是延长session有效时间
//		Cookie cookie = CookieUtil.get(request, ICookieKey.TOKEN_KEY);
//		String token = "";
//		// 防止反复刷新登录页面,多次生成不同token
//		if (cookie != null) {
//			// 用户token存在,说明已经登录过,沿用该token,延长登录有效期
//			token = cookie.getValue();
//		} else {
//			// token不存在进行用户已登录缓存
//			token = UUID.randomUUID().toString();
//		}
		
		sessionRedis.addCookieSession(response, token, userList.get(0));
		
		return token;
	}

	@Override
	public void userLogout(HttpServletRequest request, HttpServletResponse response, IRedisSession sessionRedis) {
		// 从cookie中获取token
		String token = CookieUtil.getToken(request, ICookieKey.TOKEN_KEY);
		if (StringUtils.isNotEmpty(token)) {
			sessionRedis.clearCookieSession(response, token);
		}
	}

	@Override
	public List<User> getAllUserInfo() {
		return userMapper.selectByMap(new HashMap<>());
	}
}
